﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/

using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;

namespace FX.CC.Andon.Utility.Common
{
    /// <summary>
    /// 加解密Password
    /// </summary>
    public class EncryptPassWord
    {

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="aSourceString"></param>
        /// <param name="ecd"></param>
        /// <returns></returns>
        public static string ConvertStringToBase64String(string aSourceString, Encoding ecd)
        {
            return System.Convert.ToBase64String(ecd.GetBytes(aSourceString));
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="base64Str"></param>
        /// <param name="ecd"></param>
        /// <returns></returns>
        public static string ConvertBase64StringToString(string base64Str, Encoding ecd)
        {
            var binArr = Convert.FromBase64String(base64Str);
            var rtStr = ecd.GetString(binArr, 0, binArr.Length);
            return rtStr;
        }




        //// 密钥8位
        //private static string GenerateKey()
        //{
        //    return "JLJXCKEY";
        //}

        ///// <summary>
        ///// 加密字符串
        ///// </summary>
        ///// <param name="aPassWord"></param>
        ///// <returns></returns>
        //public static string EncryptString(string aPassWord)
        //{
        //    if (string.IsNullOrEmpty(aPassWord)) return null;
        //    var key = GenerateKey();
        //    var data = Encoding.UTF8.GetBytes(aPassWord);
        //    var des = new DESCryptoServiceProvider();
        //    des.Key = Encoding.ASCII.GetBytes(key);
        //    des.IV = Encoding.ASCII.GetBytes(key);
        //    var desencrypt = des.CreateEncryptor();
        //    var result = desencrypt.TransformFinalBlock(data, 0, data.Length);
        //    return BitConverter.ToString(result);
        //}

        ///// <summary>
        ///// 解密字符串
        ///// </summary>
        ///// <param name="aPassWord"></param>
        ///// <returns></returns>
        //public static string DecryptString(string aPassWord)
        //{
        //    if (string.IsNullOrEmpty(aPassWord)) return null;
        //    var key = GenerateKey();
        //    var sInput = aPassWord.Split("-".ToCharArray());
        //    var data = new byte[sInput.Length];
        //    for (int i = 0; i < sInput.Length; i++)
        //    {
        //        data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
        //    }
        //    var des = new DESCryptoServiceProvider();
        //    des.Key = Encoding.ASCII.GetBytes(key);
        //    des.IV = Encoding.ASCII.GetBytes(key);
        //    var desencrypt = des.CreateDecryptor();
        //    var result = desencrypt.TransformFinalBlock(data, 0, data.Length);
        //    return Encoding.UTF8.GetString(result);
        //}
    }
}
